<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.categories');
jimport('joomla.application.component.modellist');

/**
 * Category Model
 */
class ProductModelProducts extends JModelList
{
	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * return	void
	 */
	protected function populateState($ordering = null, $direction = null) {
		parent::populateState($ordering, $direction);
		
		// Initialise variables.
		$this->setState('list.limit', 8);
		$this->setState('list.start', JRequest::getUInt("start"));
		
		// Setting querry string
		$q = JRequest::getString('q');
		$this->setState('q', trim(strtolower($q)));
	}
	
	/**
	 * Method to build an SQL query to load the list data.
	 *
	 * @return	string	An SQL query
	 */
	function getListQuery() {
		// Create a new query object.
		$db		= $this->getDbo();
		$query	= $db->getQuery(true);
		
		$query->select('*');
		$query->from('#__product');

		// Filter by querry string.
		if ($q = $this->getState('q')) {
			$text = $db->Quote('%' . $db->getEscaped($q, true ) . '%', false);
			$query->where('LOWER(name) LIKE ' . $text . ' OR LOWER(code) LIKE ' . $text);
		}
		
		// Filter by state
		$query->where('published = 1');

		// TODO Filter by start and end dates.

		// Set sortname ordering if selected
		$query->order('modified DESC');
		
		return $query;
	}
}